package com.gogowan.agroenterprise.widget.wheel;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.gogowan.agroenterprise.widget.wheel.AreaInfo;

public class XmlParserHandler extends DefaultHandler
{

	/**
	 * 存储所有的解析对象
	 */
	private ArrayList<AreaInfo> shengAL = new ArrayList<AreaInfo>();
	private AreaInfo provinceModel;
	private AreaInfo cityModel;
	private AreaInfo districtModel;

	public XmlParserHandler()
	{

	}

	public ArrayList<AreaInfo> getDataList()
	{
		return shengAL;
	}

	@Override
	public void startDocument() throws SAXException
	{
		// 当读到第一个开始标签的时候，会触发这个方法
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException
	{
		// 当遇到开始标记的时候，调用这个方法
		if (qName.equals("province"))
		{
			provinceModel = new AreaInfo();
			provinceModel.setAreaName(attributes.getValue(0));
			provinceModel.setAreaCode(attributes.getValue(1));
			provinceModel.setChildList(new ArrayList<AreaInfo>());
		} else if (qName.equals("city"))
		{
			cityModel = new AreaInfo();
			cityModel.setAreaName(attributes.getValue(0));
			cityModel.setAreaCode(attributes.getValue(1));
			cityModel.setChildList(new ArrayList<AreaInfo>());
		} else if (qName.equals("district"))
		{
			districtModel = new AreaInfo();
			districtModel.setAreaName(attributes.getValue(0));
			districtModel.setAreaCode(attributes.getValue(1));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException
	{
		// 遇到结束标记的时候，会调用这个方法
		if (qName.equals("district"))
		{
			cityModel.getChildList().add(districtModel);
		} else if (qName.equals("city"))
		{
			provinceModel.getChildList().add(cityModel);
		} else if (qName.equals("province"))
		{
			shengAL.add(provinceModel);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException
	{
	}

}
